//
//  ASLayoutEx.swift
//  NetEaseMusic
//
//  Created by SAGESSE on 2020/3/2.
//  Copyright © 2020 SAGESSE. All rights reserved.
//

import UIKit
import AsyncDisplayKit

extension ASLayoutElement {
    
    func insets(_ edg: UIEdgeInsets) -> ASInsetLayoutSpec {
        return ASInsetLayoutSpec(insets: edg, child: self)
    }
    func insets(top: CGFloat = 0, left: CGFloat = 0, bottom: CGFloat = 0, right: CGFloat = 0) -> ASInsetLayoutSpec {
        return insets(.init(top: top, left: left, bottom: bottom, right: right))
    }

    func flexGrow(_ flexGrow: CGFloat) -> Self {
        self.style.flexGrow = flexGrow
        return self
    }

    func flexShrink(_ flexShrink: CGFloat) -> Self {
        self.style.flexShrink = flexShrink
        return self
    }
    
    func flexBasis(_ flexBasis: ASDimension) -> Self {
        self.style.flexBasis = flexBasis
        return self
    }
}

extension ASOverlayLayoutSpec {
    
    static func overlay(_ children: [ASLayoutElement]) -> Self {
        // Initialize a layout spec.
        var last = self.init()
        last.child = children.first
        
        // If there is only one, don't to continue creating.
        if children.count < 2 {
            return last
        }

        // Merge all layout elements.
        last.overlay = children[1]
        for i in 2 ..< children.count {
            last = self.init(child: last, overlay: children[i])
        }
        return last
    }
    
    static func overlay(_ children: ASLayoutElement...) -> Self {
        return overlay(children)
    }
}

extension ASStackLayoutSpec {

    static func vertical(_ children: ASLayoutElement..., spacing: CGFloat = 0, horizontalAlignment: ASHorizontalAlignment = .none, verticalAlignment: ASVerticalAlignment = .none) -> Self {
        return vertical(children, spacing: spacing, horizontalAlignment: horizontalAlignment, verticalAlignment: verticalAlignment)
    }

    static func horizontal(_ children: ASLayoutElement..., spacing: CGFloat = 0, horizontalAlignment: ASHorizontalAlignment = .none, verticalAlignment: ASVerticalAlignment = .none) -> Self {
        return horizontal(children, spacing: spacing, horizontalAlignment: horizontalAlignment, verticalAlignment: verticalAlignment)
    }
    
    static func vertical(_ children: [ASLayoutElement]?, spacing: CGFloat = 0, horizontalAlignment: ASHorizontalAlignment = .none, verticalAlignment: ASVerticalAlignment = .none) -> Self {
        let layout = self.vertical()
        layout.children = children
        layout.spacing = spacing
        layout.horizontalAlignment = horizontalAlignment
        layout.verticalAlignment = verticalAlignment
        return layout
    }
    static func horizontal(_ children: [ASLayoutElement]?, spacing: CGFloat = 0, horizontalAlignment: ASHorizontalAlignment = .none, verticalAlignment: ASVerticalAlignment = .none) -> Self {
        let layout = self.horizontal()
        layout.children = children
        layout.spacing = spacing
        layout.horizontalAlignment = horizontalAlignment
        layout.verticalAlignment = verticalAlignment
        return layout
    }
}
